<template>
  <div class="app-container">
        <el-row :gutter="20">
          <!--案件列表数据-->
          <el-col :span="20"
                  :xs="24">
            <el-form :model="queryParams"
                     ref="queryForm"
                     size="small"
                     :inline="true"
                     v-show="showSearch"
                     label-width="68px">

              <el-form-item label="案件编号"
                            prop="contractNo">
                <el-input v-model="queryParams.caseNo"
                          placeholder="请输入案件编号"
                          clearable
                          style="width: 240px"
                          @keyup.enter.native="handleQuery" />
              </el-form-item>
              <el-form-item label="客户名称"
                            prop="contractNo">
                <el-input v-model="queryParams.customerContact"
                          placeholder="请输入客户名称"
                          clearable
                          style="width: 240px"
                          @keyup.enter.native="handleQuery" />
              </el-form-item>
              <el-form-item label="市场人员"
                            prop="contractNo">
                <el-input v-model="queryParams.empSalesName"
                          placeholder="请输入市场人员名称"
                          clearable
                          style="width: 240px"
                          @keyup.enter.native="handleQuery" />
              </el-form-item>
              <el-form-item label="撰稿人"
                            prop="empWriterName">
                <el-input v-model="queryParams.empWriterName"
                          placeholder="请输入撰稿人名称"
                          clearable
                          style="width: 240px"
                          @keyup.enter.native="handleQuery" />
              </el-form-item>
              <el-form-item label="专利类型" prop="patendTypeId">
                <el-select v-model="queryParams.patendTypeId" placeholder="专利类型" clearable>
                  <el-option
                    v-for="dict in dict.type.ipms_patend_type"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  />
                </el-select>
            </el-form-item>
              <el-form-item label="业务类型" prop="caseTtypeId">
                <el-select v-model="queryParams.patendTypeId" placeholder="业务类型" clearable>
                  <el-option
                    v-for="dict in dict.type.ipms_contract_case_type"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  />
                </el-select>
            </el-form-item>
              <el-form-item label="案件状态" prop="caseTtypeId">
                <el-select v-model="queryParams.caseStatusId" placeholder="案件状态" clearable>
                  <el-option
                    v-for="dict in dict.type.ipms_case_status"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  />
                </el-select>
            </el-form-item>

              <el-form-item>
                <el-button type="primary"
                           icon="el-icon-search"
                           size="mini"
                           @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh"
                           size="mini"
                           @click="resetQuery">重置</el-button>
              </el-form-item>
            </el-form>

            <el-table :data="caseList"
                      @selection-change="handleSelectionChange">
              <el-table-column label="案件编号"
                               align="center"
                               key="caseNo"
                               prop="caseNo"
                               v-if="columns[0].visible">
                  <template slot-scope="scope">
                    <router-link :to="{path:'/patent/case/caseDetail?caseNo='+scope.row.caseNo}">
                      <span style="color:blue">{{scope.row.caseNo}}</span>
                    </router-link>
                  </template>
              </el-table-column>

              <el-table-column label="专利名称"
                               align="center"
                               key="patendName"
                               prop="patendName"
                               v-if="columns[1].visible"
                               :show-overflow-tooltip="true" />
              <el-table-column label="专利类型"
                               align="center"
                               key="patendTypeId"
                               prop="patendTypeId"
                               :formatter="formatPatentType"
                               v-if="columns[2].visible"
                               :show-overflow-tooltip="true" >
                  <template slot-scope="scope">
                    <dict-tag :options="dict.type.ipms_patend_type" :value="scope.row.patendTypeId"/>
                  </template>
              </el-table-column>
              <el-table-column label="客户"
                               align="center"
                               key="customerContact"
                               prop="customerContact"
                               v-if="columns[3].visible"
                               :show-overflow-tooltip="true" />
              <el-table-column label="案件来源"
                               align="center"
                               width="160"
                               class-name="small-padding fixed-width">
                <span>J8Studio.meta</span>
              </el-table-column>
              <el-table-column label="业务类型"
                               align="center"
                               key="caseTtypeId"
                               prop="caseTtypeId"
                               v-if="columns[4].visible"
                               :show-overflow-tooltip="true" >
                  <template slot-scope="scope">
                    <dict-tag :options="dict.type.ipms_contract_case_type" :value="scope.row.caseTtypeId"/>
                  </template>
              </el-table-column>
              <el-table-column label="市场人员"
                               align="center"
                               key="empSalesName"
                               prop="empSalesName"
                               v-if="columns[5].visible"
                               :show-overflow-tooltip="true"
                               width="100px"/>
              <el-table-column label="案件状态"
                               align="center"
                               key="caseStatusId"
                               prop="caseStatusId"
                               v-if="columns[6].visible"
                               :show-overflow-tooltip="true" >
                  <template slot-scope="scope">
                    <dict-tag :options="dict.type.ipms_case_status" :value="scope.row.caseStatusId"/>
                  </template>
              </el-table-column>
              <el-table-column label="撰稿人"
                               align="center"
                               key="empWriterName"
                               prop="empWriterName"
                               v-if="columns[7].visible"
                               :show-overflow-tooltip="true"
                               width="200px" />
              <el-table-column label="创建时间"
                               align="center"
                               key="createTime"
                               prop="createTime"
                               v-if="columns[4].visible"
                               width="200px" />
              <el-table-column label="备注"
                               align="center"
                               key="caseReasonsRejection"
                               prop="caseReasonsRejection"
                               v-if="columns[7].visible"
                               :show-overflow-tooltip="true"
                               width="200px" />
            </el-table>
            <pagination v-show="total > 0"
                        :total="total"
                        :page.sync="queryParams.pageNum"
                        :limit.sync="queryParams.pageSize"
                        @pagination="getCaseList" />
          </el-col>
        </el-row>
      </div>
</template>

<script>
//引入题目列表方法
import {listCase,addCase,updateCase,delCase,detailCase,getEmployee,insertCaseHistory} from '@/api/patent/Bao'
export default {
  //声明使用字典
  dicts: [
    //专利类型字典
    'ipms_patend_type',
    //业务类型字典
    'ipms_contract_case_type',
    //案件状态
    'ipms_case_status',
    //业务类型字典
    'ipms_contract_case_type'
  ],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 显示搜索条件
      showSearch: true,
      // 是否显示弹出层
      open: false,
      // 弹出层标题
      title: "指定撰稿人",

      // 列信息
      columns: [
        { key: 0, visible: true },
        { key: 1, visible: true },
        { key: 2, visible: true },
        { key: 3, visible: true },
        { key: 4, visible: true },
        { key: 5, visible: true },
        { key: 6, visible: true },
        { key: 7, visible: true },
      ],

      //
      formatPatentType: undefined,

      /* 查询参数 */
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        caseNo: '',
        patendNo: undefined,
        patendName: undefined,
        patendTypeId: undefined,
        customerContact: undefined,
        caseTtypeId:undefined,
        caseStatusId: undefined,
        empSalesName: undefined,
        caseTab:undefined
      },

      //案件数据
      caseList: [],

      // 表单参数
      form: {},

      //多选框编号
      Nos: [],

      //案件id
      ids:[],

      //修改案件参数（案件状态、撰稿人）
      caseModify: {
        caseNos: [],
        caseOperation: '',
        caseEmpSalesId: undefined,
        caseEmpWriterId: undefined,
        caseReasonsRejection: undefined,
      },

      //撰稿人们
      empWriters:{},

      value:undefined,
    }
  },

  created() {
    this.getCaseList()
  },

  methods: {
    /* Tabs标签 */
    handleClick(tab, event) {
      this.queryParams.caseTab = this.activeName
      this.getCaseList()
    },

    // 表单重置
    reset() {},

    /** 搜索按钮操作 */
    handleQuery() {
      listCase(this.queryParams).then((response) => {
        this.caseList = response.rows
      })
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        caseNo: undefined,
        patendNo: undefined,
        patendName: undefined,
        patendTypeId: undefined,
        customerContact: undefined,
        caseStatusId: undefined,
        empSalesName: undefined,
      }
      this.getCaseList()
    },

    /** 重置表单 */
    restForm(){
      this.value = undefined;
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      //案件编号
      this.Nos = selection.map((item) => item.caseNo)
      //获取案件id
      this.ids = selection.map((item) => item.caseId)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },

    /* 业务方法 */
    //案件列表
    getCaseList() {
      listCase(this.queryParams).then((response) => {
        this.caseList = response.rows
        this.total = response.total
      })
    },

    //分配撰稿人点击事件
    assignment() {
      //弹窗
      this.open = true;
      //获取案件编号
      this.caseModify.caseNos = this.Nos;
      //获取撰稿人下拉框
      getEmployee().then((response) => {
        this.empWriters = response.data.data;
      })
    },


    //确定按钮
    submitForm(){
      //获取撰稿人id
      this.caseModify.caseEmpWriterId = this.value;

      //案件状态改变--0表示未分配；1表示已经分配，进入撰稿中
      this.caseModify.caseOperation = "分配撰稿人"

      updateCase(this.caseModify).then((response) => {
        this.$modal.msgSuccess('分配成功')
        //向案件历史记录表写入数据
        const caseHistoryStatusDescrip = '待撰稿';
        insertCaseHistory(this.ids,caseHistoryStatusDescrip ).then((response) => {

        });

        //重置下拉筛选框
        this.restForm();
        //关闭弹窗
        this.open = false;
        this.getCaseList();
      })
    },

    //取消按钮
    cancel(){
      //重置下拉筛选框
      this.restForm();

      //关闭弹窗
      this.open = false;
    },
  },
}
</script>


